package com.otaku.admin.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.otaku.admin.dao.entity.Permission;
import com.otaku.admin.dao.entity.Role;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PermissionMapper extends BaseMapper<Permission> {

	@Select(" select * from sys_permission ")
	List<Permission> findAllPermission();

	@Select("select * from sys_permission where id = #{id}")
	Permission findById(@Param("id") Integer id);

	/**
	 * 根据权限名获取角色名
	 * @param permId
	 * @return
	 */
	@Select("SELECT r.* FROM sys_role r INNER JOIN sys_role_permission sp on r.id = sp.role_id\n" +
			"WHERE sp.perm_id = #{permId}")
	List<Role> findRoleByPerm(@Param("permId") Integer permId);

	/**
	 * 根据用户ID获取角色名
	 * @param uId
	 * @return
	 */
	@Select("SELECT r.* FROM sys_role r INNER JOIN sys_user_role ru on r.id = ru.role_id\n" +
			"WHERE ru.user_id = #{uId}")
	List<Role> findRoleByUId(@Param("uId") Integer uId);

	/**
	 * 根据用户名获取角色名
	 * @param username
	 * @return
	 */
	@Select("SELECT r.* FROM sys_role r INNER JOIN sys_user_role ru on r.id = ru.role_id  " +
			"inner join sys_user user on r.user_id = user.id" +
			"WHERE user.username = #{username}")
	List<Role> findRoleByUserName(@Param("username") String username);

}
